<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
    <style>
        .search-btn {
            margin-left: 20px;
        }

        .tableBar {
            justify-content: flex-start !important;
        }

        .info-box {
            margin: -15px -44px 20px;
        }

        .info-box .item-box {
            display: flex;
            height: 20px;
            line-height: 20px;
            font-size: 14px;
            font-weight: 400;
            color: #666666;
            text-align: left;
            margin-bottom: 14px;
        }

        .info-box .item-box:last-child {
            margin-bottom: 0;
        }

        .info-box .item-box .label {
            width: 96px;
        }

        .info-box .item-box .des {
            flex: 1;
            color: #333333;
            width: auto;
        }
    </style>
</head>
<body>
<div class="dashboard-container" id="purchase-app" v-loading="loading">
    <div class="container">
        <!-- 搜索项 -->
        <div class="tableBar">
            <el-input v-model="purchaseId" placeholder="请输入进货单号" style="width: 250px">
                <i slot="prefix" class="el-input__icon el-icon-search" style="cursor: pointer" @click="init"></i>
            </el-input>&nbsp;&nbsp;&nbsp;
            <el-input v-model="manufacturerId" placeholder="请输入产商编号" style="width: 250px">
                <i slot="prefix" class="el-input__icon el-icon-search" style="cursor: pointer" @click="init"></i>
            </el-input>
            <el-date-picker v-model="orderTime"
                            clearable
                            value-format="yyyy-MM-dd HH:mm:ss"
                            type="datetimerange"
                            placeholder="选择日期"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            :default-time="['00:00:00', '23:59:59']"
                            style="width: 400px;margin-left: 20px;"
            ></el-date-picker>
            <el-button type="primary" class="search-btn" @click="init">查询</el-button>
        </div>

        <el-table :data="tableData" stripe class="tableBox">
            <el-table-column prop="id" label="进货单号" min-width="100px"></el-table-column>
            <el-table-column prop="status" label="状态" min-width="40px">
                <template slot-scope="{ row }">
                    <span>{{ getOrderType(row) }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="createUser" label="收货人"></el-table-column>
            <el-table-column prop="manufacturerId" label="产商编号" min-width="100px" show-overflow-tooltip></el-table-column>
            <el-table-column prop="totalCount" label="进货数量" min-width="50px"></el-table-column>
            <el-table-column prop="totalPrice" label="进货金额">
                <template slot-scope="{ row }">
                    <span>￥{{ row.totalPrice }}</span>
                </template>
            </el-table-column>

            <el-table-column
                    prop="createTime"
                    label="进货时间"
                    show-overflow-tooltip>
            </el-table-column>


            <el-table-column
                    prop="remark"
                    label="备注"
                    show-overflow-tooltip>
            </el-table-column>

            <el-table-column prop="btn" label="操作">
                <template slot-scope="{ row }">
                    <el-button type="text" @click="goDetail(row)" class="blueBug">
                        查看
                    </el-button>
                    <el-divider v-if="row.status === 2" direction="vertical"></el-divider>
                    <el-button v-if="row.status === 2" type="text" @click="cancelOrDeliveryOrComplete(3, row.id)"
                               class="blueBug">
                        派送
                    </el-button>
                    <el-divider v-if="row.status === 3" direction="vertical"></el-divider>
                    <el-button v-if="row.status === 3" type="text" @click="cancelOrDeliveryOrComplete(4, row.id)"
                               class="blueBug">
                        完成
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pageSize"
                :current-page.sync="page"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>

    <!-- 查看弹框部分 -->
    <el-dialog
            title="进货单信息"
            :visible.sync="dialogVisible"
            width="80%"
            :before-close="handleClose">
        <div class="info-box">
            <div class="item-box">
                <span class="label">订单号：</span>
                <span class="des" style="width: 40%">{{ diaForm.id }}</span>
            </div>
            <div class="item-box">
                <span class="label">订单状态：</span>
                <span class="des">{{ getOrderType(diaForm) }}</span>
            </div>
            <div class="item-box">
                <span class="label">收货人：</span>
                <span class="des">{{ diaForm.createUser }}</span>
            </div>

            <div class="item-box">
                <span class="label">进货金额：</span>
                <span class="des">￥{{ diaForm.totalPrice }}</span>
            </div>
            <div class="item-box">
                <span class="label">收货时间：</span>
                <span class="des">{{ diaForm.createTime }}</span>
            </div>
        </div>
        <el-table :data="purchaseDetail">
            <el-table-column
                    prop="name"
                    label="药品名称"/>
            </el-table-column>
            <el-table-column
                    prop="purchasePrice"
                    label="进货单价(元)"/>
            </el-table-column>

            <el-table-column
                    prop="purchaseCount"
                    label="进货数量"/>
            </el-table-column>

            <el-table-column
                    prop="totalPrice"
                    label="进货总价(元)"/>
            </el-table-column>

            <el-table-column
                    prop="produceDate"
                    label="生产日期">
                <template slot-scope="scope">
                    {{scope.row.produceDate}}
                </template>
            </el-table-column>
            <el-table-column
                    prop="effectiveDate"
                    label="有效日期">
                <template slot-scope="scope">
                    {{scope.row.effectiveDate}}
                </template>
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="200"
                    align="center">
                <template slot-scope="scope">
                    <el-button
                            type="text"
                            class="blueBug"
                            size="small"
                            @click="showDrugHandle(scope.row)">
                        药品详情
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-dialog>

    <el-dialog title="药品信息" :visible.sync="dialogTableVisible">
        <el-form ref="gridData" :model="gridData" label-width="80px">
            <el-form-item label="药品名称:">
                <el-input v-model="gridData.name" disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item label="药品编号:">
                <el-input v-model="gridData.drugId" disabled="disabled"></el-input>
            </el-form-item>

            <el-form-item label="药品编号:">
                <el-image style="width: auto; height: auto; border:none;cursor: pointer;"
                          :src="getImage(gridData.image)"
                          :preview-src-list="[ `/common/download?name=${gridData.image}` ]" >
                    <div slot="error" class="image-slot">
                        <img src="./../../images/404-images/404-cloud.png"  style="width: auto; height: 40px; border:none;" >
                    </div>
                </el-image>
            </el-form-item>

            <el-form-item label="计量单位:">
                <el-input v-model="gridData.unit" disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item label="药品类型:">
                <el-input v-model="gridData.type" disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item label="药品功效:">
                <el-input type="textarea" v-model="gridData.efficacy" disabled="disabled"></el-input>
            </el-form-item>
        </el-form>
    </el-dialog>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/purchase.js"></script>
<script src="../../api/category.js"></script>
<script src="../../api/purchaseDetail.js"></script>
<script>
    new Vue({
        el: '#purchase-app',
        data() {
            return {
                purchaseId: '',
                manufacturerId: '',
                orderTime: '',
                beginTime: '',
                endTime: '',
                counts: 0,
                page: 1,
                pageSize: 10,
                tableData: [],
                dialogVisible: false,
                diaForm: {},
                purchaseDetail: [],
                loading: false,
                dialogTableVisible: false,
                gridData: [],
                typeList:[]
            }
        },
        computed: {},
        watch: {
            orderTime(val) {
                if (val && val.length >= 2) {
                    this.beginTime = val[0]
                    this.endTime = val[1]
                } else {
                    this.beginTime = ''
                    this.endTime = ''
                }
            }
        },
        created() {
            this.init()
        },
        mounted() {
            // this.loading = false
        },
        methods: {
            async init() {
                this.getTypeList()
                getPurchasePage({
                    page: this.page,
                    pageSize: this.pageSize,
                    purchaseId: this.purchaseId || undefined,
                    manufacturerId: this.manufacturerId || undefined,
                    beginTime: this.beginTime || undefined,
                    endTime: this.endTime || undefined
                }).then(res => {
                    if (String(res.code) === '1') {
                        this.tableData = res.data.records || []
                        this.counts = res.data.total
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            getOrderType(row) {
                let str = ''
                switch (row.status) {
                    case 1:
                        str = '已入库'
                        break;
                    default:
                        str = '已退货'
                }
                return str
            },
            // 查看详情
            goDetail(row) {
                this.diaForm = {}
                this.dialogVisible = true
                this.diaForm = {...row}
                getPurchaseDetailListByPurchaseId(row.id).then(res => {
                    if (String(res.code) === '1') {
                        this.purchaseDetail = res.data
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            // 取消，派送，完成
            cancelOrDeliveryOrComplete(status, id) {
                this.$confirm('确认更改该订单状态?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    editOrderDetail(params).then(res => {
                        if (res.code === 1) {
                            this.$message.success(status === 3 ? '订单已派送' : '订单已完成')
                            this.init()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
                const params = {
                    status,
                    id
                }
            },
            showDrugHandle(data){
                this.dialogTableVisible = true
                this.gridData=data
                    for (let type of this.typeList) {
                        if (data.type==type.id) {
                            data.type=type.name
                            break
                        }
                    }
                data.unit=data.unit == '0' ? '瓶' : data.unit == '1' ? '支' : '袋'
            },
            getImage (image) {
                return `/common/download?name=${image}`
            },
            handleClose() {
                this.dialogVisible = false
            },
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val
                this.init()
            },
            getTypeList() {
                queryCategoryList().then(res => {
                    if (res.code === 1) {
                        this.typeList = res.data
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                })
            },
        }
    })
</script>
</body>
</html>